package eval.engine.cplex.InitialCplexCode;

import eval.engine.cplex.CplexMapIndexToVmId;
import eval.exceptions.CplexMapIndexToVmIdException;
import eval.utilities.configuration.ExperimentConfiguration;
import eval.vm.VMIdentifier;

public class CplexDataHolder {
	
	private ExperimentConfiguration conf = ExperimentConfiguration.getSingleton();
	
	private int[][] placementMap;
	private int[] activeHostsMap;
	
	public CplexDataHolder(CplexDataHolder prevData,
			CplexMapIndexToVmId curMap,CplexMapIndexToVmId prevMap) {
		int[] hostTmp = prevData.getActiveHostsMap();
		this.activeHostsMap = new int[hostTmp.length];
		for(int i=0;i<activeHostsMap.length;i++) {
			this.activeHostsMap[i] = hostTmp[i];
		}
		VMIdentifier[] vmTmp = curMap.getCplexIndexToVmId();
		this.placementMap = new int[vmTmp.length][];
		for(int vmIndex = 0;vmIndex<vmTmp.length; vmIndex++) {
			try {
				int prevVmIndex = prevMap.getCplexIndexFromVmId(vmTmp[vmIndex]);
				this.placementMap[vmIndex] = prevData.getPlacementMapOfVm(prevVmIndex);
			} catch (CplexMapIndexToVmIdException e) {//new deploy
				this.placementMap[vmIndex] = new int[hostTmp.length];
			}
		}
	}
	
	public CplexDataHolder (int[][] placementMap,int[] activeHostsMap) {
		if(activeHostsMap==null) {
			this.activeHostsMap = new int[conf.totalNumberOfHosts];
		} else {
			this.activeHostsMap = new int[activeHostsMap.length];
			for(int i= 0; i<activeHostsMap.length;i++)
				this.activeHostsMap[i] = activeHostsMap[i];
		}
		if(placementMap==null) {
			this.placementMap = new int[conf.totalNumberOfHosts][0];
		} else {
			this.placementMap = new int[placementMap.length][];
			for(int i= 0; i<placementMap.length;i++) {
				this.placementMap[i] = new int[placementMap[i].length];
				for(int j=0;j<placementMap[i].length;j++) {
					this.placementMap[i][j] = placementMap[i][j];
				}
			}
		}
	}
	
	public CplexDataHolder(int nbVms) {
		this.placementMap = new int[nbVms][conf.totalNumberOfHosts] ;
		this.activeHostsMap = new int[conf.totalNumberOfHosts];
	}
	
	public CplexDataHolder(int nbVms,int nbHosts) {
		this.placementMap = new int[nbVms][nbHosts] ;
		this.activeHostsMap = new int[nbHosts];
	}
	
	public CplexDataHolder() {
		this.placementMap = new int[0][conf.totalNumberOfHosts] ;
		this.activeHostsMap = new int[conf.totalNumberOfHosts];
	}
	public int[][] getPlacementMap() {
		return placementMap;
	}

	public int[] getActiveHostsMap() {
		return activeHostsMap;
	}
	
	public int getValAtActiveHostMap(int hostIndex){
		return activeHostsMap[hostIndex];
	}
	
	public int getValAtPlacementMap(int vmIndex,int hostIndex){
		return placementMap[vmIndex][hostIndex];
	}
	
	public void setPlacementMap(int[][] placementMap) {
		this.placementMap = placementMap;
	}
	
	public void setActiveHostsMap(int[] activeHostsMap) {
		this.activeHostsMap = activeHostsMap;
	}
	
	public void setData(int[][] placementMap,int[] activeHostsMap) {
		this.activeHostsMap = activeHostsMap;
		this.placementMap = placementMap;
	}
	
	public void setValAtActiveHostMap(int hostIndex,int val) {
		activeHostsMap[hostIndex] = val;
	}
	
	public void setValAtPlacementMap(int vmIndex,int hostIndex,int val) {
		placementMap[vmIndex][hostIndex] = val;
	}

	public int[] getPlacementMapOfVm(int vmIndex) {
		return placementMap[vmIndex];
	}
}
